using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.AI;


public class ArcherControllerClone : PlayerController
{
    public GameObject arrowPrefab;
    public GameObject arrowSkill1;
    public GameObject arrowSkill2;
    public GameObject arrowSkill3;
    private GameObject arrow;
    private RaycastHit hitInfo;
    public Vector3 targetPos;
    public Transform arrowStart;
    public int arrowCount;
    public Transform pos1;
    public Transform pos2;
    public GameObject ArcherClone;

    public override void OnEnable()
    {
        
    }


    public void LootToTarget()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Physics.Raycast(ray, out hitInfo);
        targetPos = hitInfo.point;
        transform.LookAt(hitInfo.point);
    }

    public Vector3 TargetPos
    {
        get => targetPos;
        set => targetPos = value;
    }


    public void Shoot()
    {
        arrow = Instantiate(arrowPrefab, arrowStart.parent);
    }

    public void Skill1Event()
    {
        arrow = Instantiate(arrowSkill1, arrowStart.parent);
        SkillManager.Instance.HaveCD[0] = true;
    }

    public void Skill2Event()
    {
        StartCoroutine("Skill2");
        SkillManager.Instance.HaveCD[1] = true;
    }

    IEnumerator Skill2()
    {
        for (int i = 0; i < arrowCount; i++)
        {
            yield return new WaitForSeconds(0.1f);
            arrow = Instantiate(arrowSkill2, transform.position + (Vector3.up * 10), Quaternion.LookRotation(targetPos-(transform.position + (Vector3.up * 10))));
        }
    }

    public void Skill3RollBack()
    {
        GameManager.Instance.playerStats.gameObject.GetComponent<NavMeshAgent>().isStopped = true;
        transform.DOMove(transform.position-(transform.forward.normalized)*5, 1);
    }

    public void SKill3Event()
    {
        for (int i = 0; i < 3; i++)
        {
            if (i == 0)
            {
                var direction = transform.forward;
                arrow = Instantiate(arrowSkill3, arrowStart.position, Quaternion.LookRotation(direction));
            }
            else if (i == 1)
            {
                var direction = 5*transform.forward+transform.right;
                arrow = Instantiate(arrowSkill3, arrowStart.position, Quaternion.LookRotation(direction));
            }
            else if (i == 2)
            {
                var direction = 5*transform.forward-transform.right;
                arrow = Instantiate(arrowSkill3, arrowStart.position, Quaternion.LookRotation(direction));
            }
        }
    }

    public void Skill4Event()
    {
        Instantiate(ArcherClone, pos1);
        Instantiate(ArcherClone, pos2);
    }
    
}
